/*
 *
 *     京东金融团队Android基础开发库积累、沉淀、封装、共同整理
 *
 *     Copyright (c) 2017. @ 京东金融移动研发团队
 *
 *     技术支持：曾繁添<zengfantian@jd.com>
 */

package com.kmshack.newsstand;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * 自定义LinearLayout，捕获onTouchMove方法，并把该事件传递给外部
 * Created by zengxianming on 2017/3/3.
 * version 1.0
 */
public class CostHEventLinearLayout extends LinearLayout {

    private static final String TAG = "CostHEventLinearLayout";
    private Point mDownPoint;
    private IScrollUpCallback mScrollUpCallback;

    public CostHEventLinearLayout(Context context) {
        super(context);
        mDownPoint = new Point();
    }

    public CostHEventLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDownPoint = new Point();
    }

    public CostHEventLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mDownPoint = new Point();
    }

    public void setScrollUpCallback(IScrollUpCallback mScrollUpCallback) {
        this.mScrollUpCallback = mScrollUpCallback;
    }

    public interface IScrollUpCallback {
        void scrollToY(int moveActionY, int downY, boolean isFromLayou);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return true;
            case MotionEvent.ACTION_MOVE:
                mScrollUpCallback.scrollToY((int) event.getY(),mDownPoint.y,true);
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownPoint.x = (int) (ev.getX() + 0.5f);
                mDownPoint.y = (int) (ev.getY() + 0.5f);
                break;
        }
        return true;//super.onInterceptTouchEvent(ev);
    }

}